package divestoclimb.checklist.storage;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import divestoclimb.checklist.data.*;

public class DatabaseInit extends SQLiteOpenHelper {
	private static final String TAG = "d2cChecklist Database";

	private static final String DATABASE_NAME="checklistdata";
	public static final int DATABASE_VERSION=5;

	public DatabaseInit(Context ctx) {
		super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		onUpgrade(db, 0, DATABASE_VERSION);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		int i;
		int v = oldVersion;
		if(v <= 6) {
			Log.w(TAG, "Upgrading database from version " + v + " to " + newVersion + ", which" +
					"will destroy all old data");
			final String tables[] = { "checklistfield", "checklistitem", "checklist",
					"templateitem", "template", "category" };
			for(i = 0; i < tables.length; i++) {
				db.execSQL("DROP TABLE IF EXISTS "+tables[i]);
			}
			final String [] create_tables_v6 = {
				"create table category ("
					+"_id integer primary key autoincrement, "
					+"Name text not null"
				+")",
				"create table template ("
					+"_id integer primary key autoincrement, "
					+"Name text not null, "
					+"Category integer references category (_id) default " + DbAdapter.KEY_CATEGORY_ID_UNFILED + ", "
					+"Version text"
				+")",
				"create table templateitem ("
					+"_id integer primary key autoincrement, "
					+"Template integer references template (_id), "
					+"DisplayOrder integer not null, "
					+"Type integer not null, "
					+"Text text not null"
				+")",
				"create table checklist ("
					+"_id integer primary key autoincrement, "
					+"Name text not null, "
					+"Category integer references category (_id) default " + DbAdapter.KEY_CATEGORY_ID_UNFILED + ", "
					+"WhenStarted text check (WhenStarted = NULL OR WhenStarted LIKE '____-__-__ __:__:__'), "
					+"WhenCompleted text check (WhenCompleted = NULL OR WhenCompleted LIKE '____-__-__ __:__:__'), "
					+"TemplateName text, "
					+"TemplateVersion text"
				+")",
				"create table checklistitem ("
					+"_id integer primary key autoincrement, "
					+"Checklist integer references checklist (_id), "
					+"DisplayOrder integer not null, "
					+"Type integer not null, "
					+"State integer not null, "
					+"WhenChanged text check (WhenChanged = NULL OR WhenChanged LIKE '____-__-__ __:__:__'), "
					+"Text text not null"
				+")",
				"create table checklistfield ("
					+"_id integer primary key autoincrement, "
					+"Item integer references checklistitem (_id), "
					+"Width integer not null, "
					+"Value text, "
					+"WhenSet text check (WhenSet = NULL OR WhenSet LIKE '____-__-__ __:__:__')"
				+")"
			};
			for(i = 0; i < create_tables_v6.length; i++) {
				db.execSQL(create_tables_v6[i]);
			}
			populateData(db);
		}
	}

	public void populateData(SQLiteDatabase db) {
		final Category[] initialCategories = {
				new Category(0, "Unfiled"),
				new Category(1, "d2cChecklist Tutorial"),
				new Category(2, "Personal"),
				new Category(3, "Work")
		};
		for(int i = 0; i < initialCategories.length; i++) {
			DbAdapter.createCategoryStatic(initialCategories[i], db);
		}
		final Template initialTemplates[] = {
				new Template(1, initialCategories[1], "About Templates - Start Here", "1.0"),
				new Template(2, initialCategories[1], "Example Template", "1.0"),
				new Template(3, initialCategories[1], "Rebreather Pre-Dive", "1.0")
		};
		for(int i = 0; i < initialTemplates.length; i++) {
			DbAdapter.createTemplateStatic(initialTemplates[i], db);
		}

		int c = DbAdapter.ITEM_CONTAINER_TEMPLATE;
		Item initialTemplateItems[] = {
			new Note(c, 1, -1, 1,
					"You're currently viewing a template for a checklist, "+
					"not an actual checklist. Keep reading to understand the "+
					"difference."
			),
			new Check(c, 1, -1, 10,
					"This is a check"
			),
			new Note(c, 1, -1, 20,
					"Checks denote something that needs to be done. To add your "+
					"own checks to this template, you can either press the Add Check "+
					"button below or long-press any item to add a check above it."
			),
			new Note(c, 1, -1, 30,
					"These are notes. You can add and remove notes from a template "+
					"in the same way as checks."
			),
			new Check(c, 1, -1, 40,
					"Reorder by dragging and dropping. Use "+
					"the bars to the left to try it."
			),
			new Check(c, 1, -1, 50,
					"When you're done here, go back and open the Example Template."
			),
			
			new Note(c, 2, -1, 1,
					"To turn this template into a checklist, select Menu -> New "+
					"Checklist. Then you will be able to fill out a copy."
			),
			new Check(c, 2, -1, 10,
					"Check me off"
			),
			new Check(c, 2, -1, 20,
					"Do not complete me"
			),
			new Note(c, 2, -1, 30,
					"To skip the previous check, long-press it and select \"Skip\"."
			),
			new Check(c, 2, -1, 40,
					"Check me off, but you noticed something odd"
			),
			new Note(c, 2, -1, 50,
					"Add a note below the previous check to document special "+
					"circumstances. To do this, long-press it and select \"Add Note.\""
			),
			new Check(c, 2, -1, 60,
					"When all checks are checked or skipped, the checklist is done."
			),
			new Note(c, 2, -1, 70,
					"If you leave one or more checks blank, you'll be able to find "+
					"this checklist under Open Checklists on the home screen."
			),
			new Note(c, 2, -1, 80,
					"If you either check or skip every item, the checklist will be "+
					"marked completed and you'll be able to find it in Completed."
			),
			
			new Note(c, 3, -1, 8,
					"DO NOT ACTUALLY USE THIS CHECKLIST. This is meant to serve as "+
					"an example only."
			),
			new Check(c, 3, -1, 16,
					"Check cylinder valves"
			),
			new Check(c, 3, -1, 24,
					"Close ADV, neg pressure test on breathing loop"
			),
			new Check(c, 3, -1, 32,
					"Add diluent, check pO2"
			),
			new Check(c, 3, -1, 40,
					"Add oxygen, check pO2, check sensor responsiveness"
			),
			new Check(c, 3, -1, 48,
					"Pos pressure test on breathing loop with dil"
			),
			new Check(c, 3, -1, 56,
					"Reopen ADV"
			),
			new Check(c, 3, -1, 64,
					"Evaluate scrubber/battery duration"
			),
			new Note(c, 3, -1, 72,
					"See scrubber replacement/battery replacement procedures"
			),
			new Check(c, 3, -1, 80,
					"Evaluate PSI of dil and O2 remaining"
			),
			new Note(c, 3, -1, 88,
					"See cylinder replacement procedure"
			),
			new Check(c, 3, -1, 96,
					"Check bailout"
			),
			new Note(c, 3, -1, 104,
					"See bailout prep"
			),
			new Check(c, 3, -1, 112,
					"Plan dive"
			),
			new Check(c, 3, -1, 120,
					"Set handset setpoints to planned pO2"
			),
			new Check(c, 3, -1, 128,
					"Set dive computer bailout mixes, setpoint"
			)
		};
		for(int i = 0; i < initialTemplateItems.length; i++) {
			DbAdapter.createTemplateItemStatic(initialTemplateItems[i], db);
		}
	}

}
